//修正滚动事件
function addEvent(ele, type, fn, capture) {
    if (ele.addEventListener) {
        ele.addEventListener(type, fn, !!capture);
    } else if (ele.attachEvent) {
        ele.attachEvent('on' + type, fn);
    }
    return fn;
}

function addWheel(ele, fn) {
    var toBind = ('onwheel' in document || document.documentMode >= 9) ? ['wheel'] : ['mousewheel', 'DOMMouseScroll'];
    for (var i in toBind) {
        addEvent(ele, toBind[i], function(e) {
            e.delta = ('wheelDelta' in e) ? e['wheelDelta'] / 120 : ('detail' in e && e['detail'] != 0) ? -e['detail'] / 3 : -e['deltaY'] / 3;
            fn.apply(ele, arguments);
        })
    }
}

var util = {
    addClass: function(o, newClass) {
        if (o.className.indexOf(newClass) == -1) {
            o.className += ' ' + newClass;
        }
    },
    removeClass: function(o, oldClass) {
        if (o.className.indexOf(oldClass) != -1) {
            o.className = o.className.replace(oldClass, '').replace(/\s+/g, ' ').replace(/^\s+|\s+$/g, '');
        }
    }
};
~ function() {
    var nums = document.querySelectorAll('.num'),
        nums_array = [].slice.call(nums),
        UP_CLASS = 'flip-up',
        DOWN_CLASS = 'flip-down';

    function init(nums) {
        for (var i in nums) {
            nums_array[i].now = nums[i];
        }
    }

    function flip(cn, t, callback) {
        for (var i in nums_array) {
            ! function(i) {
                if (nums_array[i].now == t[i]) {
                    return;
                }
                nums_array[i].now = t[i];
                util.addClass(nums_array[i], cn);
                nums_array[i].querySelector('.b-up').innerHTML = t[i];
                nums_array[i].querySelector('.b-down').innerHTML = t[i];
                var after = function() {
                    util.removeClass(nums_array[i], cn);
                    nums_array[i].querySelector('.f-up').innerHTML = t[i];
                    nums_array[i].querySelector('.f-down').innerHTML = t[i];
                }
                nums_array[i].addEventListener('webkitTransitionEnd', after);
                if (callback) {
                    callback();
                }
            }(i);
        }
    };

    function move(t) {
        function beDate(t) {
            return new Date(t.replace(/(\d{4})(\d{2})(\d{2})/, '$1,$2,$3'));
        }

        function getNow(beformatted) {
            var now = '';
            for (var i in nums_array) {
                now += nums_array[i].now;
            }
            return beformatted ? beDate(now) : now;
        }

        function pad(t, n, filling) {
            t += '';
            filling += '';
            while (t.length < n) {
                t = filling + t;
            }
            return t;
        }

        function getDateStr(now) {
            return '' + now.getFullYear() + pad(now.getMonth() + 1, 2, 0) + pad(now.getDate(), 2, 0);
        }
        var now = getNow(),
            now_date = new Date(getNow(true));
        switch (t) {
            case 'up':
            case 'next':
            case '1':
                now_date.setDate(now_date.getDate() + 1);
                flip(UP_CLASS, getDateStr(now_date));
                break;
            case 'down':
            case 'prev':
            case '-1':
                now_date.setDate(now_date.getDate() - 1);
                flip(DOWN_CLASS, getDateStr(now_date));
                break;
            default:
                if (now > t) {
                    flip(DOWN_CLASS, t);
                } else if (now < t) {
                    flip(UP_CLASS, t);
                }
        }
    };
    init('19450508');
    move();
    addWheel(document, function(e) {
        if (e.delta > 0) {
            move('up');
        } else {
            move('down');
        }
        e.preventDefault();
    });
    var prev = document.querySelector('.prev-day'),
        next = document.querySelector('.next-day');
    addEvent(prev, 'click', function() {
        move('prev')
    });
    addEvent(next, 'click', function() {
        move('next')
    })
}();
